/* eslint-disable eqeqeq */
/* eslint-disable no-eval */
/* eslint-disable camelcase */
function ueditor_markPlus(posiotonArr, that) {
  let array = []
  let length = -1
  let container = document.querySelector('iframe').contentDocument.querySelector('body')
  for (let i in container.childNodes) {
    if (container.childNodes[i].outerHTML) {
      let str = container.childNodes[i].innerText
      str = str.replace(/\n/g, '').replace(/\t/g, '')
      length += str.length
      let obj = {
        key: container.childNodes[i].outerHTML,
        value: container.childNodes[i].innerText,
        valueLength: length
      }
      array.push(obj)
    }
  }
  let posiotonArr2 = []
  posiotonArr.map((item, index) => {
    if (item.cur.length > 0) {
      for (let i = 0; i < item.cur.length; i++) {
        let corr = ''
        if (item.correct[i] && item.correct.length > item.cur.length && i + 1 == item.cur.length) {
          corr = item.correct.substr(i, item.correct.length + 1)
        } else {
          corr = item.correct[i] ? item.correct[i] : ''
        }
        let obj = {
          pos: +item.pos + i,
          cur: item.cur[i],
          corr: corr,
          description: 'word',
          before: i,
          after: item.cur.length - i - 1,
          position: item.pos
        }
        posiotonArr2.push(obj)
      }
    }
  })
  function getIndex(position) {
    for (let i in array) {
      if (array[i].valueLength >= position) {
        return i
      }
    }
  }
  posiotonArr2.map(item => { //
    let index = getIndex(item.pos) // 获取当前修改点是在HTML 数组中的哪一个位置
    // console.log(index, 10000)
    let appearNum = 0 // 记录字符串出现次数
    let arr = [] // 存储查找的位置和出现的次数
    let reg = '/' + item.cur + '/g' // 正则匹配
    let num = '' // 记录出现的第几次;
    let start_vlaue_end = '' // 记录出现的第几次出现是的前后是什么
    let str = array[index].value
    str = str.replace(/\n/g, '').replace(/\t/g, '')
    str.replace(eval(reg), function (matchStr, index1) {
      if (matchStr === item.cur) {
        appearNum++
      }
      arr.push({
        index: index1,
        num: appearNum
      })

      return matchStr
    })
    for (let i in arr) {
      if (index > 0) {
        if (item.pos == arr[i].index + 1 + array[index - 1].valueLength) {
          num = arr[i].num
          continue
        }
      } else if (item.pos == arr[i].index) {
        num = arr[i].num
        continue
      }
    }
    // `<mark data="${item.correct}" style="color:red;">${item.cur}</mark>`
    let appNum = 0 // 记录在标签里是第几次出现
    array[index].key = `${array[index].key}`.replace(eval(reg), function (matchStr, index1) {
      if (matchStr === item.cur) {
        let S_value_E = ''
        // console.log(matchStr, item.cur,index1, 232423)
        if (index1 == 0) {
          S_value_E = matchStr + array[index].key[index1 + matchStr.length]
        } else {
          S_value_E = array[index].key[index1 - 1] + matchStr
        }
        if (str.includes(S_value_E.replace(/\<.*?\>|<|>/g, ''))) {
          appNum++
          if (appNum == num) {

            item.errortext = '' //错误文字
            item.trueText = '' //正确文字
            for (var i = 0; i <= item.before + item.after; i++) {
              item.errortext += posiotonArr2[i].cur
              item.trueText += posiotonArr2[i].corr
            }
            // console.log(item)
            //     <span  class="${item.pos} ${item.position}" style="display:none;">${item.corr}</span>
            return `<span class="higspan ${item.pos}" style="padding-bottom:1px;background-color:inherit;border-bottom:2px solid red">${item.cur}</span>`
          } else {
            return matchStr
          }
        } else {
          return matchStr
        }
      } else {
        return matchStr
      }
      return matchStr
    })
  })
  let html = array.reduce((htm, cur) => {
    // console.log(htm)
    htm += cur.key
    // console.log(cur)
    return htm
  }, '')
  // console.log(html)
  /*
    *
    */
  //   container.innerHTML = html

  that.$refs.neditor._setContent(html)
  var dom = document.querySelector('iframe').contentDocument.querySelector('.view')
  // eslint-disable-next-line no-unreachable
  dom.addEventListener('click', (e) => {
    var elements = document.querySelector('iframe').contentDocument.getElementsByClassName('tips')
    for (var i = elements.length - 1; i >= 0; i--) {
      elements[i].parentNode.removeChild(elements[i])
    }

    if (!e.target.classList.value.includes('higspan')) {

    } else {

      // item.errortext = '' //错误文字
      //            item.trueText
      let errortext = ''
      let trueText = ''
      let content = {}
      posiotonArr2.forEach((item, idx) => {
        if (item.pos == e.target.classList[1]) {
          content = item
          trueText = item.trueText
          errortext = item.errortext
        }
      })


      let spanChild = document.createElement('span')
      // let cur = e.target.innerText
      // let corr = e.target.previousSibling.innerText

      spanChild.className = 'tips'
      // eslint-disable-next-line eqeqeq
      spanChild.innerHTML = `${errortext} --> ${trueText == 'null' ? '' : trueText} <span class="child_son" style="color:red;">修改</span>`
      spanChild.style.left = 14 + 'px'
      spanChild.style.background = 'white'
      spanChild.style.color = 'black'
      spanChild.style.boxShadow = '2px 7px 10px 2px #777777'
      spanChild.style.position = 'absolute'
      spanChild.style.padding = '5px'
      spanChild.style.zIndex = 1
      spanChild.style.top = e.clientY + 20 + 'px'
      spanChild.style.whiteSpace = 'nowrap'
      e.target.appendChild(spanChild)
      spanChild.addEventListener('click', (evt) => {
        if (evt.target && evt.target.className === 'child_son') {
          let node1 = e.target

          let higspanList = document.querySelector('iframe').contentDocument.querySelectorAll('.higspan')

          let idx = ''
          let higobj = {}
          for (let i = 0; i < higspanList.length; i++) {
            if (node1.className == higspanList[i].className) {
              idx = i
            }
          }
          higobj = posiotonArr2.find((item) => node1.classList[1] == item.pos)
          higobj.afters = idx + higobj.after;
          higobj.befores = idx - higobj.before;
          for (let i = higobj.befores; i <= higobj.afters; i++) {
            higspanList[i].style = ""
            higspanList[i].classList.remove('higspan')
            higspanList[i].innerText = posiotonArr2.find((item) => higspanList[i].classList[0] == item.pos).corr
          }


          posiotonArr.map((item, index) => {
            if (content.position == item.pos) {
              posiotonArr.splice(index, 1)
            }
          })

          return

          e.target.innerHTML = content.corr
          e.target.classList.remove('higspan')
          e.target.style = ''




          for (var i = 0; i < content.before; i++) {
            node1 = node1.previousSibling
            // console.log(getchildtext(node1))
            posiotonArr2.forEach((item, index) => {
              if (node1.innerText == item.cur) {
                node1.innerText = item.corr
                node1.classList.remove('higspan')
                node1.style = ''
              }
            })
          }

          let node2 = e.target
          console.log(node2)
          for (var i = 0; i < content.after; i++) {
            node2 = node2.nextSibling
            console.log(node2)
            posiotonArr2.forEach((item, index) => {
              if (node2.innerText == item.cur) {
                node2.innerText = item.corr
                node2.classList.remove('higspan')
                node2.style = ''
              }
            })
          }


          // 删除之后  删除错误数组中的数据

          console.log(errortext)
          console.log(trueText)
          console.log(content)

          console.log(posiotonArr)



          return


          let spliceindex = e.target.previousSibling.className

          posiotonArr.map((item, index) => {
            // eslint-disable-next-line eqeqeq
            if (spliceindex == item.pos) {
              posiotonArr.splice(index, 1)
            }
          })

          e.target.innerHTML = corr
          e.target.style = ''
          e.target.classList.remove('higspan')
          e.target.parentNode.removeChild(e.target.previousSibling)




        }
      })
    }
  })
}
//查询是否还有子节点
function getchildtext(node) {
  //文字和标签相同  直接返回次标签
  if (node.innerHTML == node.innerText) {
    return node
  } else {
    // 不同 在进行查找
    getchildtext(node.innerHTML)
  }
}
function modify(index, posiotonArr) {

  // console.log(index)
  // console.log(posiotonArr)
  if (document.all) {
    document
      .querySelector('iframe')
      .contentDocument.querySelector('body')
      .getElementsByClassName('highlight')[index].click()
    let spanDom = document
      .querySelector('iframe')
      .contentDocument.querySelector('body')
      .getElementsByClassName('child_son')[0]
    spanDom.click()
  } else {
    var e = document.createEvent('MouseEvent')
    e.initEvent('click', true, true)
    document
      .querySelector('iframe')
      .contentDocument.querySelector('body')
      .getElementsByClassName('higspan')[index].dispatchEvent(e)
    let spanDom = document
      .querySelector('iframe')
      .contentDocument.querySelector('body')
      .getElementsByClassName('child_son')[0]
    spanDom.click()
  }
}
// 一键修改
function oneClickModify() {

  let spanList = document
    .querySelector('iframe')
    .contentDocument.querySelector('body')
    .querySelectorAll('.higspan')
  spanList.forEach((item) => {
    item.innerText = item.previousSibling.innerText
    item.classList.remove('higspan')
    item.style = ''
    item.parentNode.removeChild(item.previousSibling)
  })
}
function inspect(that) {


  let oldhigspanList = document.querySelector('iframe').contentDocument.querySelectorAll('.higspan')
  for (let i = 0; i < oldhigspanList.length; i++) {
    oldhigspanList[i].className = ''
    oldhigspanList[i].style = ''
  }

  // 删除掉所有higspan 的类名

  let el_tab_pane = document.querySelector('#cardEdit').querySelectorAll('.el-tab-pane');
  let INDEX = 0
  el_tab_pane.forEach((item, index) => {
    if (item.style.display != 'none') {
      INDEX = index
    }
  })
  //return
  let html1 = document.querySelector('#cardEdit').querySelectorAll('.el-tab-pane')[INDEX].querySelector('iframe').contentDocument.querySelector('.view').querySelector('body').innerHTML;
  that.$refs.neditor._setContent(html1)
  let higspan = document.querySelector('iframe').contentDocument.querySelectorAll('.higspan')
  if (!higspan.length) {
    return
  }
  higspan.forEach((item) => {
    // item.parentNode.removeChild(item.previousSibling)
    let span = document.createElement('span')
    span.innerText = item.innerText
    // item.parentNode.appendChild(span)
    // item.parentNode.replaceChild(span, item)
  })
}
// export default {
//   ueditor_markPlus, modify, oneClickModify, inspect
// }
